package org.road0329.dlt698.axdr;

import org.road0329.dlt698.ReverseByteArrayInputStream;
import org.road0329.dlt698.ReverseByteArrayOutputStream;
import org.road0329.dlt698.asn1.axdr.AxdrEnum;
import org.road0329.dlt698.asn1.axdr.AxdrType;

import java.io.IOException;

public class SetRequest extends AxdrType {
    private byte[] dataCode = null;
    private Choice choice = Choice._ERR_NONE_SELECTED;
    private SetRequestNormal setRequestNormal = null;
    private SetRequestNormalList setRequestNormalList = null;
    private SetThenGetRequestNormalList setThenGetRequestNormalList = null;

    public SetRequest() {
    }

    public void set_dataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void setSetRequestNormal(SetRequestNormal setRequestNormal) {
        resetChoices();
        this.choice = Choice.SET_REQUEST_NORMAL;
        this.setRequestNormal = setRequestNormal;
    }

    public void setSetRequestNormalList(SetRequestNormalList setRequestNormalList) {
        resetChoices();
        this.choice = Choice.SET_REQUEST_NORMAL_LIST;
        this.setRequestNormalList = setRequestNormalList;
    }

    public void setSetThenGetRequestNormalList(SetThenGetRequestNormalList setThenGetRequestNormalList) {
        resetChoices();
        this.choice = Choice.SET_THEN_GET_REQUEST_NORMAL_LIST;
        this.setThenGetRequestNormalList = setThenGetRequestNormalList;
    }

    public int decode(ReverseByteArrayInputStream input) throws Exception {
        int codeLength = 0;
        AxdrEnum choosen = new AxdrEnum();
        choosen.setConst();
        codeLength += choosen.decode(input);
        resetChoices();
        this.choice = Choice.getEnumByValue((int) choosen.getValue());
        if (this.choice == Choice.SET_REQUEST_NORMAL) {
            this.setRequestNormal = new SetRequestNormal();
            codeLength += this.setRequestNormal.decode(input);
            return codeLength;
        }
        if (this.choice == Choice.SET_REQUEST_NORMAL_LIST) {
            this.setRequestNormalList = new SetRequestNormalList();
            codeLength += this.setRequestNormalList.decode(input);
            return codeLength;
        }
        if (this.choice == Choice.SET_THEN_GET_REQUEST_NORMAL_LIST) {
            this.setThenGetRequestNormalList = new SetThenGetRequestNormalList();
            codeLength += this.setThenGetRequestNormalList.decode(input);
            return codeLength;
        }
        throw new Exception("Error decoding AxdrChoice: Identifier matched to no item.");
    }

    public int encode(ReverseByteArrayOutputStream output) throws Exception {
        if (this.dataCode != null) {
            for (int i = this.dataCode.length - 1; i >= 0; i--) {
                output.write(this.dataCode[i]);
            }
            return this.dataCode.length;
        }
        if (this.choice == Choice._ERR_NONE_SELECTED) {
            throw new Exception("Error encoding AxdrChoice: No item in choice was selected.");
        }
        int codeLength = 0;
        if (this.choice == Choice.SET_THEN_GET_REQUEST_NORMAL_LIST) {
            if (this.setThenGetRequestNormalList != null) {
                codeLength += this.setThenGetRequestNormalList.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(3);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new Exception("setThenGetRequestNormalList is null");
            }
        } else if (this.choice == Choice.SET_REQUEST_NORMAL_LIST) {
            if (this.setRequestNormalList != null) {
                codeLength += this.setRequestNormalList.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(2);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new Exception("setRequestNormalList is null");
            }
        } else if (this.choice == Choice.SET_REQUEST_NORMAL) {
            if (this.setRequestNormal != null) {
                codeLength += this.setRequestNormal.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(1);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new Exception("setRequestNormal is null");
            }
        }
        throw new Exception("Error encoding AxdrChoice: No item in choice was encoded.");
    }

    private void resetChoices() {
        this.choice = Choice._ERR_NONE_SELECTED;
        this.setRequestNormal = null;
        this.setRequestNormalList = null;
        this.setThenGetRequestNormalList = null;
    }

    public void encodeAndSave(int encodingSizeGuess) throws Exception {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    public Choice getChoiceIndex() {
        return this.choice;
    }

    public String toString() {
        if (this.choice == Choice.SET_REQUEST_NORMAL) {
            if (this.setRequestNormal != null) {
                return "choice: {setRequestNormal: " + this.setRequestNormal + "}";
            } else {
                return "choice is setRequestNormal but setRequestNormal is null";
            }
        } else if (this.choice == Choice.SET_REQUEST_NORMAL_LIST) {
            if (this.setRequestNormalList != null) {
                return "choice: {setRequestNormalList: " + this.setRequestNormalList + "}";
            } else {
                return "choice is setRequestNormalList but setRequestNormalList is null";
            }
        } else if (this.choice == Choice.SET_THEN_GET_REQUEST_NORMAL_LIST) {
            if (this.setThenGetRequestNormalList != null) {
                return "choice: {setThenGetRequestNormalList: " + this.setThenGetRequestNormalList + "}";
            } else {
                return "choice is setThenGetRequestNormalList but setThenGetRequestNormalList is null";
            }
        } else {
            return "unknown";
        }
    }

    public enum Choice {
        _ERR_NONE_SELECTED(-1),
        SET_REQUEST_NORMAL(1),
        SET_REQUEST_NORMAL_LIST(2),
        SET_THEN_GET_REQUEST_NORMAL_LIST(3);

        private final int value;

        Choice(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }

        public static Choice getEnumByValue(int value) {
            for (Choice item : Choice.values()) {
                if (value == item.getValue()) {
                    return item;
                }
            }
            return null;
        }
    }
}



